package com.google.gwt.it2tool.client.widget;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MassProductionPanel extends HorizontalPanel {
	
	private ArrayList<IMassProductionChangedHandler> changeHandlers = new ArrayList<IMassProductionChangedHandler>();
	
	//Header
	private Label setMassProductionLabel = new Label("Massenproduktion einstellen");
	//Header+content
	private VerticalPanel body = new VerticalPanel();
	private HorizontalPanel content = new HorizontalPanel();
	private Label massProductionLabel = new Label("Massenproduktion:");
	private ListBox massProductionListBox = new ListBox();	
	
	public MassProductionPanel(){
		super();
		
		massProductionLabel.addStyleName("massProductionLabel");
		setMassProductionLabel.addStyleName("setMassProductionLabel");
				
		for (int i=0;i<21;i++){
			massProductionListBox.addItem(Integer.toString(i));
		}
		
		massProductionListBox.setVisibleItemCount(1);
		massProductionListBox.addChangeHandler(new ChangeHandler() {			
			public void onChange(ChangeEvent event) {
				notifyChangeHandlers(massProductionListBox.getSelectedIndex());			
			}
		});
		
		body.add(setMassProductionLabel);
		body.add(content);
		content.add(massProductionLabel);
		content.add(massProductionListBox);	
		
		add(body);
		addStyleName("massProductionPanel");
	}
	
	public void addMassProductionChangedHandler(IMassProductionChangedHandler handler){
		if (handler!=null){
			changeHandlers.add(handler);
		}
	}
	
	public void removeMassProductionChangedHandler(IMassProductionChangedHandler handler){
		if (handler!=null && changeHandlers.contains(handler)){
			changeHandlers.remove(handler);
		}
	}
	
	public int getMassProductionLvl(){
		return massProductionListBox.getSelectedIndex();
	}
	
	public void setMassProductionLvl(int newLvl){
		massProductionListBox.setSelectedIndex(newLvl);
	}
	
	private void notifyChangeHandlers(int newLvl){
		for(IMassProductionChangedHandler handler: changeHandlers){
			handler.onMassProductionChanged(newLvl);
		}
	}
	
	public interface IMassProductionChangedHandler{
		public void onMassProductionChanged(int newLvl);
	}
}
